package bb.lanxing.device.sgdevcie.sensors;

import android.content.ContentValues;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import org.litepal.LitePal;

import bb.lanxing.App;
import bb.lanxing.lib.devices.api.DeviceProvider;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.core.scanner.BleSmartDeviceScanner;
import bb.lanxing.lib.devices.core.scanner.DeviceScanner;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;
import bb.lanxing.lib.devices.sprint.presenter.SearchSprintPresenter;
import bb.lanxing.lib.devices.sprint.view.SearchSprintView;
import bb.lanxing.lib.devices.utils.BleUtils;
import bb.lanxing.model.database.Device;
//import bb.lanxing.util.Log;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
//import javax.inject.Inject;

public class SearchSprintPresenterImpl implements DeviceScanner.ScannerCallback<SmartDevice>, SearchSprintPresenter {
    final private static String TAG = "SearchSprintPresenterImpl";
    private Map<String, SmartDevice> mScannedDevices;
    DeviceScanner mScanner;
    private SearchSprintView mView;
    int searchDeviceType;
    private final int[] sensorType = Device.getTypeSensorTypes();
    private final int[] sensorCadenceAndSpeedType = Device.getTypeCadenceAndSpeedTypes();
    private final int[] sensorCadenceOrSpeedType = Device.getTypeCadenceOrSpeedTypes();
    private final int[] sensorHeartRateType = Device.getTypeHeartRateTypes();
    private final int[] sensorPowerRateType = Device.getTypePowerRateTypes();
    private final int[] sensorBikeTrainerType = Device.getTypeBikeTrainerTypes();
    private final int[] sensorIndoorBikeType = Device.getTypeIndoorBikeTypes();
    private final int[] sensorCrossTrainerType = Device.getTypeCrossTrainerTypes();
    private final int[] sensorRowerType = Device.getTypeRowerTypes();

    public SearchSprintPresenterImpl(Context context, DeviceProvider deviceProvider, SearchSprintView searchSprintView) {
        this.mView = searchSprintView;
        this.mScanner = new BleSmartDeviceScanner(context, this, deviceProvider);
    }

    public SearchSprintPresenterImpl(Context context, DeviceProvider deviceProvider, SearchSprintView searchSprintView, int i) {
        this.mView = searchSprintView;
        this.searchDeviceType = i;
        this.mScanner = new BleSmartDeviceScanner(context, this, deviceProvider, i);
    }

    public void startScan(long j) {
        if (this.mScanner.isScanning()) {
            return;
        }
        Map<String, SmartDevice> map = this.mScannedDevices;
        if (map != null) {
            map.clear();
        } else {
            this.mScannedDevices = new LinkedHashMap();
        }
        SearchSprintView searchSprintView = this.mView;
        if (searchSprintView != null) {
            searchSprintView.notifyDataChanged();
        }
        this.mScanner.startScan(j);
    }

    @Override
    public void startScan() {
        startScan(-1L);
    }

    @Override
    public void stopScan() {
        DeviceScanner deviceScanner = this.mScanner;
        if (deviceScanner != null) {
            deviceScanner.stopScan();
        }
    }

    @Override
    public SmartDevice getDevice(int i) {
        int i2 = 0;
        for (String str : this.mScannedDevices.keySet()) {
            int i3 = i2 + 1;
            if (i2 == i) {
                return this.mScannedDevices.get(str);
            }
            i2 = i3;
        }
        return null;
    }

    @Override
    public SmartDevice getDevice(int i, int i2) {
        if (hasBoundDevice() && i == 0) {
            List<SmartDevice> boundDevices = getBoundDevices();
            if (i2 >= boundDevices.size()) {
                return null;
            }
            return boundDevices.get(i2);
        }
        int i3 = 0;
        for (String str : this.mScannedDevices.keySet()) {
            int i4 = i3 + 1;
            if (i3 == i2) {
                return this.mScannedDevices.get(str);
            }
            i3 = i4;
        }
        return null;
    }

    @Override
    public void clear() {
        Map<String, SmartDevice> map = this.mScannedDevices;
        if (map != null) {
            map.clear();
            SearchSprintView searchSprintView = this.mView;
            if (searchSprintView == null) {
                return;
            }
            searchSprintView.notifyDataChanged();
        }
    }

    @Override
    public void destroy() {
        stopScan();
        DeviceScanner deviceScanner = this.mScanner;
        if (deviceScanner != null) {
            deviceScanner.release();
            this.mScanner = null;
        }
        Map<String, SmartDevice> map = this.mScannedDevices;
        if (map != null) {
            map.clear();
            this.mScannedDevices = null;
            SearchSprintView searchSprintView = this.mView;
            if (searchSprintView == null) {
                return;
            }
            searchSprintView.notifyDataChanged();
        }
    }

    @Override
    public boolean isScanning() {
        DeviceScanner deviceScanner = this.mScanner;
        return deviceScanner != null && deviceScanner.isScanning();
    }

    @Override
    public int getRssi(String str) {
        SmartDevice smartDevice = this.mScannedDevices.get(str);
        if (smartDevice == null) {
            return 0;
        }
        return smartDevice.getProtocol() == 1 ? smartDevice.getRssi() : 0;
    }

    @Override
    public int getDeviceCount() {
        Map<String, SmartDevice> map = this.mScannedDevices;
        if (map != null) {
            return map.size();
        }
        return 0;
    }

    @Override
    public void setSearchDeviceType(int i) {
        this.searchDeviceType = i;
    }

    private void updateDevice(SmartDevice smartDevice) {
        String address = smartDevice.getAddress();
        if (TextUtils.isEmpty(address)) {
            return;
        }
        int size = (hasBoundDevice() ? getBoundDevices().size() + 1 : 0) + 1;
        this.mScannedDevices.put(address, smartDevice);
        Iterator<String> it = this.mScannedDevices.keySet().iterator();
        while (it.hasNext() && !it.next().equals(address)) {
            size++;
        }
        this.mView.notifyDeviceUpdate(size);
    }

    @Override
    public void onStartScan() {
        SearchSprintView searchSprintView = this.mView;
        if (searchSprintView != null) {
            searchSprintView.onStartScan();
        }
    }

    @Override
    public void onStopScan() {
        SearchSprintView searchSprintView = this.mView;
        if (searchSprintView != null) {
            searchSprintView.onStopScan();
        }
    }

    @Override
    public void onDevice(SmartDevice paramSmartDevice) {
        String stringBuilder = "|| on search || device: " +
                paramSmartDevice.getName() +
                " add: " +
                paramSmartDevice.getAddress();
        Log.d(TAG, stringBuilder);
        String str = paramSmartDevice.getAddress();
        int m = BleUtils.getDeviceType(paramSmartDevice, paramSmartDevice.getScanRecord());
        int n = this.searchDeviceType;
        Log.d(TAG, "onDevice this.searchDeviceType=" + this.searchDeviceType + ", BleUtils.getDeviceType=" + m);
        byte b1 = 0;
        int j = 0;
        int i = 0;
        byte b2 = 0;
        int k = 0;
        if (n == SmartDevice.TYPE_SENSORS) {
            int[] arrayOfInt = this.sensorType;
            j = arrayOfInt.length;

            while (true) {
                b1 = b2;
                if (i < j) {
                    if (arrayOfInt[i] == m)
                        b2 = 1;
                    i++;
                    continue;
                }
                break;
            }
        } else if (n == SmartDevice.TYPE_SENSOR_CADENCE_AND_SPEED) {
            int[] arrayOfInt = this.sensorCadenceAndSpeedType;
            j = arrayOfInt.length;

            while (true) {
                b1 = b2;
                if (i < j) {
                    if (arrayOfInt[i] == m)
                        b2 = 1;
                    i++;
                    continue;
                }
                break;
            }
        } else if (n == SmartDevice.TYPE_SENSOR_CADENCE_OR_SPEED) {
            int[] arrayOfInt = this.sensorCadenceOrSpeedType;
            k = arrayOfInt.length;

            while (true) {
                b1 = b2;
                if (i < k) {
                    if (arrayOfInt[i] == m)
                        b2 = 1;
                    i++;
                    continue;
                }
                break;
            }
        } else if (n == SmartDevice.TYPE_SENSOR_HEART_RATE) {
            int[] arrayOfInt = this.sensorHeartRateType;
            j = arrayOfInt.length;

            while (true) {
                b1 = b2;
                if (i < j) {
                    if (arrayOfInt[i] == m)
                        b2 = 1;
                    i++;
                    continue;
                }
                break;
            }
        } else if (n == SmartDevice.TYPE_BIKE_TRAINER) {
            int[] arrayOfInt = this.sensorBikeTrainerType;
            j = arrayOfInt.length;

            while (true) {
                b1 = b2;
                if (i < j) {
                    if (arrayOfInt[i] == m)
                        b2 = 1;
                    i++;
                    continue;
                }
                break;
            }
        } else if (n == SmartDevice.TYPE_POWERRATE) {
            int[] arrayOfInt = this.sensorPowerRateType;
            j = arrayOfInt.length;

            while (true) {
                b1 = b2;
                if (i < j) {
                    if (arrayOfInt[i] == m)
                        b2 = 1;
                    i++;
                    continue;
                }
                break;
            }
        }else if (n == SmartDevice.TYPE_CROSS_TRAINER) {
            int[] arrayOfInt = this.sensorCrossTrainerType;
            j = arrayOfInt.length;

            while (true) {
                b1 = b2;
                if (i < j) {
                    if (arrayOfInt[i] == m)
                        b2 = 1;
                    i++;
                    continue;
                }
                break;
            }
        }else if (n == SmartDevice.TYPE_ROWER) {
            int[] arrayOfInt = this.sensorRowerType;
            j = arrayOfInt.length;

            while (true) {
                b1 = b2;
                if (i < j) {
                    if (arrayOfInt[i] == m)
                            b2 = 1;
                        i++;
                        continue;
                    }
                    break;
            }
        }else if (n == SmartDevice.TYPE_INDOOR_BIKE) {
            int[] arrayOfInt = this.sensorIndoorBikeType;
            j = arrayOfInt.length;

            while (true) {
                b1 = b2;
                if (i < j) {
                    if (arrayOfInt[i] == m)
                        b2 = 1;
                    i++;
                    continue;
                }
                break;
            }
        }else {
            if (m == n)
                b1 = 1;
        }
        if (b1 != 0) {
            paramSmartDevice.setType(m);
            if (!this.mScannedDevices.containsKey(str)) {
                this.mScannedDevices.put(str, paramSmartDevice);
                this.mView.notifyInsertedDevice();
            } else {
                updateDevice(paramSmartDevice);
            }
        }
    }
    @Override
    public boolean hasBoundDevice() {
        return Device.hasThisTypeDeviceBound(this.searchDeviceType);
    }

    @Override
    public List<SmartDevice> getBoundDevices() {
        return new ArrayList<>(Device.getDevicesByLogicType(this.searchDeviceType));
    }

    @Override
    public void unboundDevice(SmartDevice smartDevice) {
        if (!(smartDevice instanceof Device)) {
            return;
        }
        ((Device) smartDevice).delete();
        DeviceHelper.disconnect(smartDevice.getAddress());
        this.mView.notifyDataChanged();
    }

    @Override
    public boolean isConnected(String str) {
        return !TextUtils.isEmpty(str) && DeviceHelper.isConnected(str);
    }

    @Override
    public boolean needSaveDevice(SmartDevice saveDevice) {
        boolean needAutoConnect = Device.isThisTypeNeedAutoConnect(saveDevice.getType());
        if (needAutoConnect && (saveDevice instanceof Device)) {
            String address = saveDevice.getAddress();
            Device existDevice = Device.getByAddress(address);
            Log.d(TAG, "needSaveDevice: address=" + address + ", existDevice=" + existDevice);
            if (existDevice != null) {
                Log.d(TAG, "needSaveDevice: existDevice id=" + existDevice.getId() + ",rssi=" + existDevice.getRssi());
                Log.d(TAG, "needSaveDevice: saveDevice name=" + saveDevice.getName()
                        + ",rssi=" + saveDevice.getRssi() + ",Flags=" + saveDevice.getFlags() + ",Type=" + saveDevice.getType());
                ContentValues contentValues = new ContentValues();
                contentValues.put("name", saveDevice.getName());
                contentValues.put("rssi", saveDevice.getRssi());
                if (saveDevice.getFlags() > 0) {
                    contentValues.put("flags", saveDevice.getFlags());
                }
                contentValues.put("type", saveDevice.getType());
                LitePal.update(Device.class, contentValues, existDevice.getId());
            } else {
                Log.d(TAG, "needSaveDevice: save directly.");
                ((Device) saveDevice).save();
            }
        }
        return needAutoConnect;
    }
}
